package regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ChangLiang
 * @date 2020/2/18
 */
public abstract class RegexUtils {

    /**
     * use precompile
     */
    private static Pattern pattern = Pattern.compile("\\d*[1-9](0*)?");

    /**
     * Non-greedy match
     *
     * 给定一个字符串表示的数字，判断该数字末尾0的个数
     * Given a number represented by a string, determine the number of 0 at the end of the number
     * 123000 -- 3
     * 1001 -- 0
     * 10100 -- 2
     * @return
     */
    public static int getEndNumberZeroCount(int number) {
        if (number < 0) {
            throw new IllegalArgumentException();
        }
        if (number == 0) {
            return 1;
        }
        Matcher matcher = pattern.matcher(String.valueOf(number));
        if (matcher.matches()) {
            return matcher.group(1).length();
        }else {
            return 0;
        }
    }

    public static boolean isValidFixTelephone(String s) {
        // very rough rule
        return s.matches("\\d{3,4}-\\d{7,8}");
    }
}
